import { Component, OnInit, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ServiceProxy } from '@shared/service-proxy/service-proxies';
import { NzTableHelper } from '@shared/helpers/NzTableHelper';
import { finalize } from 'rxjs/operators';
import { RealOnlineModalComponent } from '../real-online-modal/real-online-modal.component';

@Component({
  selector: 'app-view-arrived-plan-modal',
  templateUrl: './view-arrived-plan-modal.component.html',
  styleUrls: ['./view-arrived-plan-modal.component.less']
})
export class ViewArrivedPlanModalComponent extends AppComponentBase implements OnInit {
  @ViewChild('realOnlineModal') realOnlineModal: RealOnlineModalComponent;
  isVisible: boolean = false;
  params: string;
  tableHelper: NzTableHelper;
  routeParams: any;
  constructor(injector: Injector,
    private _service: ServiceProxy) {
    super(injector);
    this.tableHelper = new NzTableHelper();
  }


  ngOnInit() {
    this.routeParams = {
      launchsiteId: '', destinationsiteId: '', containerCode: '', containerStatus: [1, 3, 5],arrived:'true'
    };
  }

  load(pageIndex?: number) {
    if (typeof pageIndex !== 'undefined') {
      this.tableHelper.pageIndex = pageIndex || 1;
    }
    this.getList();
  }

  sort(sort: { key: string, value: string }): void {
    this.tableHelper.setSorting = sort;
    this.load();
  }

  getList() {
    if (this.routeParams) {
      this.routeParams.containerStatus = [3];
      this.params = JSON.stringify(this.routeParams);
    }

    this._service.searchAllDispatchPageUsingGET(this.tableHelper.pageIndex, this.tableHelper.pageSize, this.params
    ).pipe(finalize(() => {
      this.tableHelper.loading = false;
    })).subscribe((result) => {
      this.tableHelper.items = result.result.fieldList;
      this.tableHelper.totalCount = result.result.totalRow;
    });
  }


  show() {
    this.isVisible = true;
    this.load();
  }


  handleCancel() {
    this.isVisible = false;
  }

  handleOk() {
    this.isVisible = false;
  }

  showAddress(id: string, code: string) {
    this.realOnlineModal.show(id, code);
  }
}
